package team.knowers.qzhserver.course.course.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.knowers.qzhserver.course.course.entity.dao.CourseDO;
import team.knowers.qzhserver.course.course.entity.dto.CourseReleaseDTO;
import team.knowers.qzhserver.course.course.entity.dto.CourseUpdateDTO;
import team.knowers.qzhserver.course.course.mapper.CourseMapper;
import team.knowers.qzhserver.course.course.service.CourseCacheService;
import team.knowers.qzhserver.course.course.service.CourseService;
import team.knowers.qzhserver.course.datum.datum.service.DatumService;
import team.knowers.qzhserver.user.user.mapper.UserMapper;

import java.util.Date;

/**
 * @author zcb
 * @date 2024/3/6 14:00
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public CourseDO createCourse(String uid, String name) {
        //防止不合理请求
        if(userMapper.selectById(uid)==null)
            return null;
        CourseDO courseDO = CourseDO.builder()
                .userId(uid)
                .name(name)
                .gmtCreate(new Date())
                .gmtModified(new Date())
                .build();
        courseMapper.insert(courseDO);
        return courseDO;
    }

    @Override
    public CourseDO getCourseById(String id) {
        //先从缓存中查找课程
        CourseDO courseDO = getCacheService().getCourse(id);
        if(courseDO == null){
            //当缓存中找不到时，从数据库中查找
            courseDO = courseMapper.selectById(id);
            if(courseDO == null){
                //数据库也找不到就直接退出
                return null;
            }
        }
        //更新缓存并返回数据
        getCacheService().setCourse(courseDO);
        return courseDO;
    }

    @Override
    public Boolean updateCourse(CourseUpdateDTO courseUpdateDTO) {
        CourseDO courseDO = getCourseById(courseUpdateDTO.getId());
        courseDO.setName(courseUpdateDTO.getName());
        courseDO.setCover(courseUpdateDTO.getCover());
        courseDO.setSubjectId(courseUpdateDTO.getSubjectId());
        courseDO.setDescription(courseUpdateDTO.getDescription());
        courseDO.setGmtModified(new Date());
        getCacheService().setCourse(courseDO);
        return courseMapper.updateById(courseDO) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteCourse(String id) throws Exception {
        CourseDO courseDO = getCourseById(id);
        if(courseDO==null){
            return false;
        }
        //连锁删除属于该课程的所有资料库
        for(String cdid:getDatumService().getCourseDatumSet(id)){
            getDatumService().getCacheService().delDatum(cdid);
            if(!getDatumService().deleteDatum(cdid)){
                throw new Exception("删除资料库时发生异常");
            }
        }
        getDatumService().getCacheService().delCourseDatumSetAll(id);
        //删除课程自身的数据
        getCacheService().delCourse(id);
        if(courseMapper.deleteById(id)==1){
            return true;
        }else {
            throw new Exception("删除课程时发生异常");
        }
    }

    /**
     * 请记住！所有对现有对象做更新、删除操作的时候务必也对缓存中的对象一起更新和删除
     */
    @Override
    public CourseCacheService getCacheService() {
        return SpringUtil.getBean(CourseCacheService.class);
    }

    @Override
    public Boolean releaseCourse(CourseReleaseDTO courseReleaseDTO) {
        CourseDO courseDO = getCourseById(courseReleaseDTO.getId());
        courseDO.setPrice(courseReleaseDTO.getPrice());
        courseDO.setTrySee(courseReleaseDTO.getTrySee());
        courseDO.setGmtModified(new Date());
        courseDO.setStatus(1);
        getCacheService().setCourse(courseDO);
        return courseMapper.updateById(courseDO) == 1;
    }
    private DatumService getDatumService(){return SpringUtil.getBean(DatumService.class);}

}
